/*
Copyright (C) 2013-2021 LZE
Maximum Flow Problem:Edmond-Karp
*/
#include<bits/stdc++.h>
#define INF 0x7fffffff
#define mod 1000000007
#define eps 1e-6
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N = 410;
const int M = 10010;
ll n,m,s,t;
struct NF_Edge{
    ll to,next,w;
}edge[M];
ll num = 1; // ^1
ll head[N] = {0};
void add(ll u,ll v,ll w){
    edge[++num].to = v;
    edge[num].w = w;
    edge[num].next = head[u];
    head[u] = num;
}

ll last[M] = {0}; // from..
ll flows[N] = {0};
inline ll bfs(){
    memset(last,-1,sizeof(last));
    queue<ll>q;
    q.push(s);
    flows[s] = INF;
    while(!q.empty()){
        ll p = q.front(); q.pop();
        if(p == t)
            break;
        for(ll i = head[p];i;i = edge[i].next){
            ll to = edge[i].to,val = edge[i].w;
            // to:point node   val:edge flow
            if(last[to] == -1 && val > 0){
                last[to] = i;
                flows[to] = min(val,flows[p]);
                q.push(to);
            }
        }
    }
    return last[t] != -1;
}
inline ll Edmond_Karp(){
    ll max_flow = 0;
    while(bfs()){
        max_flow += flows[t];
        for(ll i = t;i != s;i = edge[last[i] ^ 1].to){
            edge[last[i]].w -= flows[t];
            edge[last[i] ^ 1].w += flows[t];
        }
    }
    return max_flow;
}

int main() {
	scanf("%lld%lld%lld%lld",&n,&m,&s,&t);
    ll u,v,w;
    for(ll i = 1;i <= m;i++){
        scanf("%lld%lld%lld",&u,&v,&w);
        add(u,v,w);
        add(v,u,0);
    }
    printf("%lld\n",Edmond_Karp());
	return 0;
}
